package com.opencloud.base.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opencloud.base.client.model.AuthorityMenu;
import com.opencloud.base.client.model.entity.BaseRole;
import com.opencloud.base.client.model.entity.Tenant;
import com.opencloud.base.client.service.ITenantServiceClient;
import com.opencloud.base.server.service.BaseAuthorityService;
import com.opencloud.base.server.service.BaseRoleService;
import com.opencloud.base.server.service.TenantService;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.common.security.OpenHelper;
import com.opencloud.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 租户表 前端控制器
 *
 * @author admin
 * @date 2021-03-02
 */
@Api(value = "租户表", tags = "租户表")
    @RestController
    public class TenantController implements ITenantServiceClient {

    @Autowired
    private TenantService targetService;
    @Autowired
    private BaseAuthorityService baseAuthorityService;
    /**
    * 获取分页数据
    *
    * @return
    */
    @ApiOperation(value = "获取分页数据", notes = "获取分页数据")
    @GetMapping(value = "/tenant/list")
    public ResultBody<IPage<Tenant>>list(@RequestParam(required = false) Map map){
        PageParams pageParams = new PageParams(map);
        Tenant query = pageParams.mapToObject(Tenant.class);
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper();
        return ResultBody.ok().data(targetService.page(pageParams,queryWrapper));
    }

    /**
     * 根据ID查找数据
     */
    @ApiOperation(value = "根据ID查找数据", notes = "根据ID查找数据")
    @ResponseBody
    @RequestMapping("/tenant/get")
    public ResultBody<Tenant> get(@RequestParam("id") Long id){
        Tenant entity = targetService.getById(id);
        return ResultBody.ok().data(entity);
    }
    @Autowired
    private BaseRoleService baseRoleService;
    /**
     * 获取租户角色
     *
     * @param tenantId
     * @return
     */
    @Override
    @ApiOperation(value = "获取租户已分配角色", notes = "获取用户已分配角色")
    @GetMapping("/tenant/getRolesByTenantId")
    public ResultBody<List<BaseRole>> getRolesByTenantId(
            @RequestParam(value = "tenantId") String tenantId
    ) {
        return ResultBody.ok().data(baseRoleService.getTenantRoles(tenantId));
    }

    @ApiOperation(value = "获取当前登录租户已分配菜单权限", notes = "获取当前登录租户已分配菜单权限")
    @GetMapping("/tenant/user/menu")
    @Override
    public ResultBody<List<AuthorityMenu>> findAuthorityMenu() {
        List<AuthorityMenu> result = baseAuthorityService.findAuthorityMenuByTenant(OpenHelper.getUser().getTenantId());
        return ResultBody.ok().data(result);
    }

    /**
    * 添加数据
    * @return
    */
    @ApiOperation(value = "添加数据", notes = "添加数据")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "tenantId", required = true, value = "租户ID", paramType = "form"),
         @ApiImplicitParam(name = "tenantName", required = true, value = "租户名称", paramType = "form"),
         @ApiImplicitParam(name = "linkman", required = true, value = "联系人", paramType = "form"),
         @ApiImplicitParam(name = "contactNumber", required = true, value = "联系电话", paramType = "form"),
         @ApiImplicitParam(name = "speciaTaxRate", required = true, value = "专票税费率(%)", paramType = "form"),
         @ApiImplicitParam(name = "serviceRate", required = true, value = "服务费率(%)", paramType = "form"),
         @ApiImplicitParam(name = "address", required = true, value = "联系地址", paramType = "form"),
         @ApiImplicitParam(name = "createUser", required = true, value = "创建人", paramType = "form"),
         @ApiImplicitParam(name = "updateUser", required = true, value = "修改人", paramType = "form"),
         @ApiImplicitParam(name = "status", required = true, value = "状态", paramType = "form"),
         @ApiImplicitParam(name = "isDeleted", required = true, value = "是否已删除", paramType = "form"),
        @ApiImplicitParam(name = "type", required = true, value = "1 个人 2企业 3小微", paramType = "form")
            })
    @PostMapping("/tenant/add")
    public ResultBody add(
        @RequestParam(value = "tenantId") String tenantId,
        @RequestParam(value = "tenantName") String tenantName,
        @RequestParam(value = "linkman") String linkman,
        @RequestParam(value = "contactNumber") String contactNumber,
        @RequestParam(value = "speciaTaxRate") BigDecimal speciaTaxRate,
        @RequestParam(value = "serviceRate") BigDecimal serviceRate,
        @RequestParam(value = "address") String address,
        @RequestParam(value = "createUser") Integer createUser,
        @RequestParam(value = "updateUser") Integer updateUser,
        @RequestParam(value = "status") Integer status,
        @RequestParam(value = "isDeleted") Integer isDeleted,
        @RequestParam(value = "type") Integer type
            ){
        Tenant entity = new Tenant();
        entity.setTenantId(tenantId);
        entity.setTenantName(tenantName);
        entity.setLinkman(linkman);
        entity.setContactNumber(contactNumber);
        entity.setSpeciaTaxRate(speciaTaxRate);
        entity.setServiceRate(serviceRate);
        entity.setAddress(address);
        entity.setCreateUser(createUser);
        entity.setUpdateUser(updateUser);
        entity.setStatus(status);
        entity.setIsDeleted(isDeleted);
        entity.setType(type);
        targetService.save(entity);
        return ResultBody.ok();
    }

    /**
    * 更新数据
    * @return
    */
    @ApiOperation(value = "更新数据", notes = "更新数据")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "id", required = true, value = "主键", paramType = "form"),
                    @ApiImplicitParam(name = "tenantId", required = true, value = "租户ID", paramType = "form"),
                    @ApiImplicitParam(name = "tenantName", required = true, value = "租户名称", paramType = "form"),
                    @ApiImplicitParam(name = "linkman", required = true, value = "联系人", paramType = "form"),
                    @ApiImplicitParam(name = "contactNumber", required = true, value = "联系电话", paramType = "form"),
                    @ApiImplicitParam(name = "speciaTaxRate", required = true, value = "专票税费率(%)", paramType = "form"),
                    @ApiImplicitParam(name = "serviceRate", required = true, value = "服务费率(%)", paramType = "form"),
                    @ApiImplicitParam(name = "address", required = true, value = "联系地址", paramType = "form"),
                    @ApiImplicitParam(name = "createUser", required = true, value = "创建人", paramType = "form"),
                    @ApiImplicitParam(name = "updateUser", required = true, value = "修改人", paramType = "form"),
                    @ApiImplicitParam(name = "status", required = true, value = "状态", paramType = "form"),
                    @ApiImplicitParam(name = "isDeleted", required = true, value = "是否已删除", paramType = "form"),
                    @ApiImplicitParam(name = "type", required = true, value = "1 个人 2企业 3小微", paramType = "form")
        })
        @PostMapping("/tenant/update")
        public ResultBody add(
                @RequestParam(value = "id") Integer id,
                @RequestParam(value = "tenantId") String tenantId,
                @RequestParam(value = "tenantName") String tenantName,
                @RequestParam(value = "linkman") String linkman,
                @RequestParam(value = "contactNumber") String contactNumber,
                @RequestParam(value = "speciaTaxRate") BigDecimal speciaTaxRate,
                @RequestParam(value = "serviceRate") BigDecimal serviceRate,
                @RequestParam(value = "address") String address,
                @RequestParam(value = "createUser") Integer createUser,
                @RequestParam(value = "updateUser") Integer updateUser,
                @RequestParam(value = "status") Integer status,
                @RequestParam(value = "isDeleted") Integer isDeleted,
                @RequestParam(value = "type") Integer type
        ){
            Tenant entity = new Tenant();
                    entity.setId(id);
                    entity.setTenantId(tenantId);
                    entity.setTenantName(tenantName);
                    entity.setLinkman(linkman);
                    entity.setContactNumber(contactNumber);
                    entity.setSpeciaTaxRate(speciaTaxRate);
                    entity.setServiceRate(serviceRate);
                    entity.setAddress(address);
                    entity.setCreateUser(createUser);
                    entity.setUpdateUser(updateUser);
                    entity.setStatus(status);
                    entity.setIsDeleted(isDeleted);
                    entity.setType(type);
                targetService.updateById(entity);
                return ResultBody.ok();
        }

    /**
    * 删除数据
    * @return
    */
    @ApiOperation(value = "删除数据", notes = "删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "id", paramType = "form")
    })
    @PostMapping("/tenant/remove")
    public ResultBody remove(
            @RequestParam(value = "id") Long id
    ){
            targetService.removeById(id);
            return ResultBody.ok();
      }

    /**
    * 批量删除数据
    * @return
    */
    @ApiOperation(value = "批量删除数据", notes = "批量删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    @PostMapping("/tenant/batch/remove")
    public ResultBody batchRemove(
                @RequestParam(value = "ids") String ids
            ){
            targetService.removeByIds(Arrays.asList(ids.split(",")));
            return ResultBody.ok();
     }

    /**
     * 用户分配角色
     *
     * @param tenantId
     * @param roleIds
     * @return
     */
    @ApiOperation(value = "租户分配角色", notes = "租户分配角色")
    @PostMapping("/tenant/roles/add")
    public ResultBody addUserRoles(
            @RequestParam(value = "tenantId") String tenantId,
            @RequestParam(value = "roleIds", required = false) String roleIds) {
        baseRoleService.saveTenantRoles(tenantId, StringUtils.isNotBlank(roleIds) ? roleIds.split(",") : new String[]{});
        return ResultBody.ok();
    }
}
